package com.forlove.app.qselect.friend.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.forlove.app.R;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.common.widget.recyview.BaseRecyclerAdapter;
import com.forlove.app.common.widget.recyview.ViewHolder;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.entity.NewsFriendsStatus;
import com.forlove.app.qselect.friend.entity.SearchFriendEntity;
import com.forlove.app.qselect.friend.presenter.MineNewFriendPresenter;
import com.forlove.app.qselect.friend.ui.SearchUserInfoActivity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新的好友请求
 * Created by yjl on 2017/10/14.
 */

public class FriendNewAskAdapter extends BaseRecyclerAdapter<MineFriendEntity> {
    private MineNewFriendPresenter presenter;
    private int showCount = -1;//

    /**
     * 默认全显示
     *
     * @param context
     * @param itemView
     * @param mDatas
     * @param presenter
     */
    public FriendNewAskAdapter(Context context, int itemView, List<MineFriendEntity> mDatas,
                               MineNewFriendPresenter presenter) {
        super(context, itemView, mDatas);
        this.presenter = presenter;
        this.showCount = -1;
    }

    /**
     * 显示showCount条数据，-1全显示
     *
     * @param context
     * @param itemView
     * @param mDatas
     * @param presenter
     * @param showCount
     */
    public FriendNewAskAdapter(Context context, int itemView,
                               List<MineFriendEntity> mDatas,
                               MineNewFriendPresenter presenter,
                               int showCount) {
        super(context, itemView, mDatas);
        this.presenter = presenter;
        this.showCount = showCount;
    }

    @Override
    protected void convertView(ViewHolder holder, final MineFriendEntity entity, int position) {
        presenter.loadImageView(mContext,
                holder.getView(R.id.view_friend_photo),
                entity.img, R.mipmap.image_mine_friend_avatar);
        holder.setText(R.id.text_add_name, entity.realname);
        if (!TextUtils.isEmpty(entity.msg)) {
            holder.setText(R.id.text_add_info, entity.msg);
            holder.setVisible(R.id.text_add_info, true);
            holder.setVisible(R.id.text_friend_from, false);
        } else {
            holder.setVisible(R.id.text_add_info, false);
            holder.setText(R.id.text_friend_from, "申请添加你为好友");
            holder.setVisible(R.id.text_friend_from, true);
        }
        TextView status = holder.getView(R.id.view_friend_status);

        if (entity.status == 0) {
            status.setBackgroundResource(R.drawable.bt_agree_add_selector);
            status.setText("同意");
            status.setTextColor(mContext.getResources().getColor(R.color.common_green));
        } else if (entity.status == 1) {
            status.setBackgroundResource(R.color.white);
            status.setText("已添加");
            status.setTextColor(mContext.getResources().getColor(R.color.str_color_gray));
            holder.setText(R.id.text_friend_from, entity.mobile);
            if (StringUtils.isNotEmpty(entity.contactsName)) {
                holder.setText(R.id.text_friend_from, "手机联系人：" + entity.contactsName);
            } else if (StringUtils.isNotEmpty(entity.username)) {
                holder.setText(R.id.text_friend_from, "圈一圈：" + entity.username);
            } else {
                holder.setText(R.id.text_friend_from, entity.mobile);
            }
        } else if (entity.status == -1) {
            status.setBackgroundResource(R.drawable.bt_invite_selector);
            status.setText("添加");
            status.setTextColor(mContext.getResources().getColor(R.color.white));
            if (StringUtils.isNotEmpty(entity.contactsName)) {
                holder.setText(R.id.text_friend_from, "手机联系人：" + entity.contactsName);
            } else if (StringUtils.isNotEmpty(entity.username)) {
                holder.setText(R.id.text_friend_from, "圈一圈：" + entity.username);
            } else {
                holder.setText(R.id.text_friend_from, entity.mobile);
            }
        } else if (entity.status == NewsFriendsStatus.WAITING.getCode()) {
            status.setBackgroundResource(R.color.white);
            status.setText("等待验证");
            status.setTextColor(mContext.getResources().getColor(R.color.str_color_gray));
            if (StringUtils.isNotEmpty(entity.contactsName)) {
                holder.setText(R.id.text_friend_from, "手机联系人：" + entity.contactsName);
            } else if (StringUtils.isNotEmpty(entity.username)) {
                holder.setText(R.id.text_friend_from, "圈一圈：" + entity.username);
            } else {
                holder.setText(R.id.text_friend_from, entity.mobile);
            }
        }
        if (entity.status == 0 || entity.status == -1) {
            status.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (entity.status == 0) {
                        //同意好友请求
                        Map param = new HashMap();
                        param.put("friend", entity.mobile);
                        presenter.loadAgreeFriendData(param);
                    } else if (entity.status == -1) {
                        //添加好友，好友申请页面
                        SearchFriendEntity searchFriendEntity = new SearchFriendEntity();
                        searchFriendEntity.friendEntity = entity;
                        searchFriendEntity.isFriend = false;
                        searchFriendEntity.note_times = entity.charm;
                        Intent intent = new Intent(mContext, SearchUserInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("friendinfo", searchFriendEntity);
                        bundle.putString("mobile", entity.getAvaliableMobile());
                        intent.putExtras(bundle);
                        mContext.startActivity(intent);
                    }
                }
            });
        } else {
            status.setOnClickListener(null);
        }

    }

    @Override
    public int getItemCount() {
        if (showCount == -1) {
            return super.getItemCount();
        } else {
            if (mDatas == null || mDatas.size() == 0) {
                return 1;
            } else {
                if (mDatas.size() > showCount) {
                    return 3;
                }
                return mDatas.size();
            }
        }
    }

}
